
/****************************************************************************
**
** Copyright (C) 2004-2008 VATATA.com . All rights reserved.
**
** This file is the part of the Miniutil Poject of VATATA.com.
** Vatata's miniutil library is lightweight and portable (for linux
** and Windows). The C++ Class Library included Threadpool, Dll danymic loading, 
** General socket application model and tcp server, httpserver/servlet, etc. 
** It is a pratical utility disigned for who don't want learn the Big C++ 
** Developing Platform, such as  Boost or ACE.
**
** This file may be used under the terms of the GNU General Public
** License versions 2.0 or 3.0 as published by the Free Software
** Foundation and appearing in the files LICENSE.GPL2 and LICENSE.GPL3
** included in the packaging of this file.  Alternatively you may (at
** your option) use any later version of the GNU General Public
** License if such license has been publicly approved by VATATA.com.
** (or its successors, if any) and the Miniutil Project Aministrators. 
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://www.gnu.org/licenses/lgpl.html. If you are unsure which 
** license is appropriate for your use, please review the following 
** information: http://code.google.com/p/miniutil/ or contact 
** http://www.vatata.com.
**
** In addition, as a special exception, VATATA.com, as the sole
** copyright holder for Miniutil Project, grants users of VATATA
** P2P Platform the right to release and package with the related 
** libraries without the source code.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. vatata reserves all rights not expressly
** granted herein.
**
** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
**
****************************************************************************/


#ifndef __MINIUTIL__URL__H__
#define __MINIUTIL__URL__H__

#include <string>
#include <map>

using namespace std;

namespace miniutil {


		class url
		{
		protected:
			string queryString;
			map<string, string> queryParam;
			void clear();

		public:
			string urlstr;
			string proto;
			string host;
			int port;
			string uri;
			string baseuri;

			inline url( const char * urlStr = NULL )
			{
				this->parseURL( urlStr );
			}
			inline url( const string & urlStr )
			{
				this->parseURL( urlStr );
			}
			inline url( const url & theUrl ) 
			{
				this->parseURL( theUrl.urlstr );
			}

			string & getURL();
			string getBaseURL();
			int parseURL(const char * urlStr );
			int parseURL(const string &urlStr );

			int parseURI(const char * urlStr );
			int parseURI(const string &urlStr );

			static string decode( char * str , int len = -1 );
			static string encode( char * str , int len = -1 );
			static string decode(const string & str );
			static string encode(const string & str );

			static int defaultPort( string proto );

			inline string getParam(const char * key )
			{
				return getParam( string(key) );
			}
			inline string getParam(const string & key )
			{
				if ( queryParam.find(key) == queryParam.end() )
					return string("");

				return decode(queryParam[ (key) ]);
			}

			inline string getRawParam( const char * key )
			{
				if ( queryParam.find(key) == queryParam.end() )
					return string("");

				return (queryParam[ (key) ]);
			}

            inline int getAllParams(std::map<std::string, std::string>& allpara)
            {
                map<string, string>::iterator iter_ss;
                for(iter_ss = queryParam.begin(); iter_ss != queryParam.end(); iter_ss ++)
                {
                    allpara.insert(std::make_pair(iter_ss->first, decode(iter_ss->second)));
                }
                return 0;
            }

			void setParam( char * key, char * value );
			void setParam( const string &key, const string &value);
			void setQueryString( const string &queryStr );
			string & getQueryString();

		};


}

#endif
